/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.converters;

import cz.muni.fi.pa165.machineryrental.dto.RentalDTO;
import cz.muni.fi.pa165.machineryrental.entities.Rental;

/**
 *
 * @author 
 */
public class RentalDTOConverter {
    
    /**
     * Converts Rental entity to RentalDTO
     */
    public static RentalDTO toDTO(Rental entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        RentalDTO dto = new RentalDTO();
        return applyToDTO(dto, entity);
	}

    /**
     * Converts RentalDTO to Rental entity
     */
    public static Rental toEntity(RentalDTO dto) {
        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        Rental entity = new Rental();
        entity.setId(dto.getId());
        entity.setDateOfRent(dto.getDateOfRent());
        entity.setDateOfReturn(dto.getDateOfReturn());
        entity.setDuration(dto.getDuration());
		
		if (dto.getCustomer() != null) {
			entity.setCustomer(CustomerDTOConverter.toEntity(dto.getCustomer()));
		}
		
		if (entity.getMachine() != null) {
			entity.setMachine(MachineDTOConverter.toEntity(dto.getMachine()));
		}
		
        return entity;
    }

    /**
     * Overwrites DTO with information in entity
     */
    public static RentalDTO applyToDTO(RentalDTO dto, Rental entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        dto.setId(entity.getId());
        dto.setDateOfRent(entity.getDateOfRent());
        dto.setDateOfReturn(entity.getDateOfReturn());
        dto.setDuration(entity.getDuration());

		if (dto.getCustomer() != null) {
			dto.setCustomer(CustomerDTOConverter.toDTO(entity.getCustomer()));
		}
		
		if (entity.getMachine() != null) {
			dto.setMachine(MachineDTOConverter.toDTO(entity.getMachine()));
		}

        return dto;
    }
    
}
